﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.ComponentModel;

namespace CooStationsToolsNew
{
    public class MyCanvasAdorner : Adorner
    {
        const double THUMB_SIZE = 12;
        const double MINIMAL_SIZE = 20;
        const double MOVE_OFFSET = 20;
        ModelTools model = new ModelTools();
        Thumb tl, tr, bl, br,rc,lc,tc,bc;
        Thumb mov;
        VisualCollection visCollec;
        
        bool flag;
        public MyCanvasAdorner(UIElement adorned,bool _flag)
            : base(adorned)
        {
            flag = _flag;
            visCollec = new VisualCollection(this);
            if (!flag)
            {
                visCollec.Add(mov = GetMoveThumb());
            }
            else
            {
                visCollec.Add(tl = GetResizeThumb(Cursors.SizeNWSE, HorizontalAlignment.Left, VerticalAlignment.Top));
                visCollec.Add(tr = GetResizeThumb(Cursors.SizeNESW, HorizontalAlignment.Right, VerticalAlignment.Top));
                visCollec.Add(bl = GetResizeThumb(Cursors.SizeNESW, HorizontalAlignment.Left, VerticalAlignment.Bottom));
                visCollec.Add(br = GetResizeThumb(Cursors.SizeNWSE, HorizontalAlignment.Right, VerticalAlignment.Bottom));
                visCollec.Add(rc = GetResizeThumb(Cursors.SizeWE, HorizontalAlignment.Right, VerticalAlignment.Center));
                visCollec.Add(lc = GetResizeThumb(Cursors.SizeWE, HorizontalAlignment.Left, VerticalAlignment.Center));
                visCollec.Add(tc = GetResizeThumb(Cursors.SizeNS, HorizontalAlignment.Center, VerticalAlignment.Top));
                visCollec.Add(bc = GetResizeThumb(Cursors.SizeNS, HorizontalAlignment.Center, VerticalAlignment.Bottom));
                visCollec.Add(mov = GetMoveThumb());
            }
           
        }
        protected override Size ArrangeOverride(Size finalSize)
        {
            double offset = THUMB_SIZE / 2;
            Size sz = new Size(THUMB_SIZE, THUMB_SIZE);
            if (!flag) {
                mov.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width / 2 - THUMB_SIZE / 2, -(MOVE_OFFSET * 2)), sz));
                mov.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            }
            else
            {
                tl.Arrange(new Rect(new Point(-offset, -offset), sz));
                tl.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
                tr.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width - offset, -offset), sz));
                tr.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
                bl.Arrange(new Rect(new Point(-offset, AdornedElement.RenderSize.Height - offset), sz));
                bl.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
                br.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width - offset, AdornedElement.RenderSize.Height - offset), sz));
                br.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
                rc.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width - offset, (AdornedElement.RenderSize.Height - offset) / 2), sz));
                rc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
                lc.Arrange(new Rect(new Point(-offset, (AdornedElement.RenderSize.Height - offset) / 2), sz));
                lc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
                tc.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width / 2 - THUMB_SIZE / 2, -offset), sz));
                tc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
                bc.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width / 2 - THUMB_SIZE / 2, AdornedElement.RenderSize.Height - offset), sz));
                bc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
                mov.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width / 2 - THUMB_SIZE / 2, -(MOVE_OFFSET * 2)), sz));
                mov.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            }
            return finalSize;
        }

        void tl_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try { model.SaveToImageData(App.uiobj as Image, App.dt); }
            catch { }
            try { model.SaveToRectData(App.uiobj as Rectangle, App.dt); }
            catch { }
            try { model.SaveToDivData(App.uiobj as Canvas, App.dt); }
            catch { }
            try { model.SaveToTextData(App.uiobj as TextBlock, App.dt); }
            catch { }
        }

        void Resize(FrameworkElement ff)
        {
            if (Double.IsNaN(ff.Width))
                ff.Width = ff.RenderSize.Width;
            if (Double.IsNaN(ff.Height))
                ff.Height = ff.RenderSize.Height;
        }

        Thumb GetMoveThumb()
        {
            var thumb = new Thumb()
            {
                Width = THUMB_SIZE,
                Height = THUMB_SIZE,
                Cursor = Cursors.SizeAll,
                Template = new ControlTemplate(typeof(Thumb))
                {
                    VisualTree = GetFactory(new SolidColorBrush(Colors.Red))
                }
            };
            thumb.DragDelta += (s, e) =>
            {
                var element = AdornedElement as FrameworkElement;
                if (element == null)
                    return;

                Canvas.SetLeft(element, double.Parse((Canvas.GetLeft(element) + e.HorizontalChange).ToString("F0")));
                Canvas.SetTop(element, double.Parse((Canvas.GetTop(element) + e.VerticalChange).ToString("F0")));
            };
            return thumb;
        }

        Thumb GetResizeThumb(Cursor cur, HorizontalAlignment hor, VerticalAlignment ver)
        {
            var thumb = new Thumb()
            {
               
                Background = Brushes.Red,
                Width = THUMB_SIZE,
                Height = THUMB_SIZE,
                HorizontalAlignment = hor,
                VerticalAlignment = ver,
                Cursor = cur,
                Template = new ControlTemplate(typeof(Thumb))
                {
                    VisualTree = GetFactory(GetMoveEllipseBack())
                }
            };
            if (flag)
            {
                thumb.DragDelta += (s, e) =>
                {
                    var element = AdornedElement as FrameworkElement;
                    if (element == null)
                        return;

                    Resize(element);

                    switch (thumb.VerticalAlignment)
                    {
                        case VerticalAlignment.Bottom:
                            if (element.Height + e.VerticalChange > MINIMAL_SIZE)
                            {
                                element.Height += double.Parse(e.VerticalChange.ToString("F0"));
                            }
                            break;
                        case VerticalAlignment.Top:
                            if (element.Height - e.VerticalChange > MINIMAL_SIZE)
                            {
                                element.Height -= double.Parse(e.VerticalChange.ToString("F0"));
                                Canvas.SetTop(element, double.Parse((Canvas.GetTop(element) + e.VerticalChange).ToString("F0")));
                            }
                            break;
                    }
                    switch (thumb.HorizontalAlignment)
                    {
                        case HorizontalAlignment.Left:
                            if (element.Width - e.HorizontalChange > MINIMAL_SIZE)
                            {
                                element.Width -= double.Parse(e.HorizontalChange.ToString("F0"));
                                Canvas.SetLeft(element, double.Parse((Canvas.GetLeft(element) + e.HorizontalChange).ToString("F0")));
                            }
                            break;
                        case HorizontalAlignment.Right:
                            if (element.Width + e.HorizontalChange > MINIMAL_SIZE)
                            {
                                element.Width += double.Parse(e.HorizontalChange.ToString("F0"));
                            }
                            break;
                    }

                    e.Handled = true;
                };
            }
            return thumb;
        }
        Brush GetMoveEllipseBack()
        {
            string lan = "M 0,5 h 10 M 5,0 v 10";
            var converter = TypeDescriptor.GetConverter(typeof(Geometry));
            var geometry = (Geometry)converter.ConvertFrom(lan);
            TileBrush bsh = new DrawingBrush(new GeometryDrawing(Brushes.Transparent, new Pen(Brushes.White, 0), geometry));
            bsh.Stretch = Stretch.Fill;
            return bsh;
           
        }

        FrameworkElementFactory GetFactory(Brush back)
        {
            
            back.Opacity = 1.0;
            var fef = new FrameworkElementFactory(typeof(Ellipse));
            fef.SetValue(Ellipse.FillProperty, back);
            fef.SetValue(Ellipse.StrokeProperty, Brushes.Gray);
            fef.SetValue(Ellipse.StrokeThicknessProperty, (double)1);
            return fef;
            
        }

        FrameworkElementFactory GetLRTBFactory(Brush back)
        {
            back.Opacity = 0;
            var fef = new FrameworkElementFactory(typeof(Ellipse));
            fef.SetValue(Ellipse.FillProperty, back);
            fef.SetValue(Ellipse.StrokeProperty, Brushes.Gray);
            fef.SetValue(Ellipse.StrokeThicknessProperty, (double)1);
            return fef;
        }

        protected override Visual GetVisualChild(int index)
        {
            return visCollec[index];
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return visCollec.Count;
            }
        }

    }

    class AWCanvasAdorner : Adorner
    {
        ModelTools model = new ModelTools();
        const double THUMB_SIZE = 12;
        const double MINIMAL_SIZE = 20;
        const double MOVE_OFFSET = 20;
        Thumb tl, tr, bl, br, rc, lc, tc, bc;
        Thumb mov;
        VisualCollection visCollec;
        public AWCanvasAdorner(UIElement adorned)
            : base(adorned)
        {
            visCollec = new VisualCollection(this);
            visCollec.Add(tl = GetResizeThumb(Cursors.SizeNWSE, HorizontalAlignment.Left, VerticalAlignment.Top));
            visCollec.Add(tr = GetResizeThumb(Cursors.SizeNESW, HorizontalAlignment.Right, VerticalAlignment.Top));
            visCollec.Add(bl = GetResizeThumb(Cursors.SizeNESW, HorizontalAlignment.Left, VerticalAlignment.Bottom));
            visCollec.Add(br = GetResizeThumb(Cursors.SizeNWSE, HorizontalAlignment.Right, VerticalAlignment.Bottom));
            visCollec.Add(rc = GetResizeThumb(Cursors.SizeWE, HorizontalAlignment.Right, VerticalAlignment.Center));
            visCollec.Add(lc = GetResizeThumb(Cursors.SizeWE, HorizontalAlignment.Left, VerticalAlignment.Center));
            visCollec.Add(tc = GetResizeThumb(Cursors.SizeNS, HorizontalAlignment.Center, VerticalAlignment.Top));
            visCollec.Add(bc = GetResizeThumb(Cursors.SizeNS, HorizontalAlignment.Center, VerticalAlignment.Bottom));
            visCollec.Add(mov = GetMoveThumb());
        }



        protected override Size ArrangeOverride(Size finalSize)
        {
            double offset = THUMB_SIZE / 2;
            Size sz = new Size(THUMB_SIZE, THUMB_SIZE);
            tl.Arrange(new Rect(new Point(-offset, -offset), sz));
           // tl.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            tr.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width - offset, -offset), sz));
           // tr.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            bl.Arrange(new Rect(new Point(-offset, AdornedElement.RenderSize.Height - offset), sz));
          //  bl.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            br.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width - offset, AdornedElement.RenderSize.Height - offset), sz));
           // br.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            rc.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width - offset, (AdornedElement.RenderSize.Height - offset) / 2), sz));
           // rc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            lc.Arrange(new Rect(new Point(-offset, (AdornedElement.RenderSize.Height - offset) / 2), sz));
           // lc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            tc.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width / 2 - THUMB_SIZE / 2, -offset), sz));
           // tc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            bc.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width / 2 - THUMB_SIZE / 2, AdornedElement.RenderSize.Height - offset), sz));
            //bc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            mov.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width / 2 - THUMB_SIZE / 2, -(MOVE_OFFSET * 2)), sz));
          //  mov.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            return finalSize;
        }

        void tl_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try { model.SaveToImageData(App.uiobj as Image, App.dt); }
            catch { }
            try { model.SaveToRectData(App.uiobj as Rectangle, App.dt); }
            catch { }
          

        }

        void Resize(FrameworkElement ff)
        {
            if (Double.IsNaN(ff.Width))
                ff.Width = ff.RenderSize.Width;
            if (Double.IsNaN(ff.Height))
                ff.Height = ff.RenderSize.Height;
        }

        Thumb GetMoveThumb()
        {
            var thumb = new Thumb()
            {
                Width = THUMB_SIZE,
                Height = THUMB_SIZE,
                Cursor = Cursors.SizeAll,
                Template = new ControlTemplate(typeof(Thumb))
                {
                    VisualTree = GetFactory(new SolidColorBrush(Colors.Green))
                }
            };
            thumb.DragDelta += (s, e) =>
            {
                var element = AdornedElement as FrameworkElement;
                if (element == null)
                    return;

                Canvas.SetLeft(element, double.Parse((Canvas.GetLeft(element) + e.HorizontalChange).ToString("F0")));
                Canvas.SetTop(element, double.Parse((Canvas.GetTop(element) + e.VerticalChange).ToString("F0")));
            };
            return thumb;
        }

        Thumb GetResizeThumb(Cursor cur, HorizontalAlignment hor, VerticalAlignment ver)
        {
            var thumb = new Thumb()
            {
                Background = Brushes.Red,
                Width = THUMB_SIZE,
                Height = THUMB_SIZE,
                HorizontalAlignment = hor,
                VerticalAlignment = ver,
                Cursor = cur,
                Template = new ControlTemplate(typeof(Thumb))
                {
                    VisualTree = GetFactory(GetMoveEllipseBack())
                }
            };
            thumb.DragDelta += (s, e) =>
            {
                var element = AdornedElement as FrameworkElement;
                if (element == null)
                    return;

                Resize(element);

                switch (thumb.VerticalAlignment)
                {
                    case VerticalAlignment.Bottom:
                        if (element.Height + e.VerticalChange > MINIMAL_SIZE)
                        {
                            element.Height += double.Parse(e.VerticalChange.ToString("F0"));
                        }
                        break;
                    case VerticalAlignment.Top:
                        if (element.Height - e.VerticalChange > MINIMAL_SIZE)
                        {
                            element.Height -= double.Parse(e.VerticalChange.ToString("F0"));
                            Canvas.SetTop(element, double.Parse((Canvas.GetTop(element) + e.VerticalChange).ToString("F0")));
                        }
                        break;
                }
                switch (thumb.HorizontalAlignment)
                {
                    case HorizontalAlignment.Left:
                        if (element.Width - e.HorizontalChange > MINIMAL_SIZE)
                        {
                            element.Width -= double.Parse(e.HorizontalChange.ToString("F0"));
                            Canvas.SetLeft(element, double.Parse((Canvas.GetLeft(element) + e.HorizontalChange).ToString("F0")));
                        }
                        break;
                    case HorizontalAlignment.Right:
                        if (element.Width + e.HorizontalChange > MINIMAL_SIZE)
                        {
                            element.Width += double.Parse(e.HorizontalChange.ToString("F0"));
                        }
                        break;
                }

                e.Handled = true;
            };
            return thumb;
        }
        Brush GetMoveEllipseBack()
        {
            string lan = "M 0,5 h 10 M 5,0 v 10";
            var converter = TypeDescriptor.GetConverter(typeof(Geometry));
            var geometry = (Geometry)converter.ConvertFrom(lan);
            TileBrush bsh = new DrawingBrush(new GeometryDrawing(Brushes.Transparent, new Pen(Brushes.White, 2), geometry));
            bsh.Stretch = Stretch.Fill;
            return bsh;
        }

        FrameworkElementFactory GetFactory(Brush back)
        {
            back.Opacity = 0.6;
            var fef = new FrameworkElementFactory(typeof(Ellipse));
            fef.SetValue(Ellipse.FillProperty, back);
            fef.SetValue(Ellipse.StrokeProperty, Brushes.Gray);
            fef.SetValue(Ellipse.StrokeThicknessProperty, (double)1);
            return fef;
        }

        protected override Visual GetVisualChild(int index)
        {
            return visCollec[index];
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return visCollec.Count;
            }
        }

    }

    class AWShowCanvasAdorner : Adorner
    {
        const double THUMB_SIZE = 5;
        const double MINIMAL_SIZE = 20;
        const double MOVE_OFFSET = 20;
        ModelTools model = new ModelTools();
        Thumb tl, tr, bl, br, rc, lc, tc, bc;
        Thumb mov;
        VisualCollection visCollec;
        public AWShowCanvasAdorner(UIElement adorned)
            : base(adorned)
        {
            visCollec = new VisualCollection(this);
            visCollec.Add(tl = GetResizeThumb(Cursors.SizeNWSE, HorizontalAlignment.Left, VerticalAlignment.Top));
            visCollec.Add(tr = GetResizeThumb(Cursors.SizeNESW, HorizontalAlignment.Right, VerticalAlignment.Top));
            visCollec.Add(bl = GetResizeThumb(Cursors.SizeNESW, HorizontalAlignment.Left, VerticalAlignment.Bottom));
            visCollec.Add(br = GetResizeThumb(Cursors.SizeNWSE, HorizontalAlignment.Right, VerticalAlignment.Bottom));
            visCollec.Add(rc = GetResizeThumb(Cursors.SizeWE, HorizontalAlignment.Right, VerticalAlignment.Center));
            visCollec.Add(lc = GetResizeThumb(Cursors.SizeWE, HorizontalAlignment.Left, VerticalAlignment.Center));
            visCollec.Add(tc = GetResizeThumb(Cursors.SizeNS, HorizontalAlignment.Center, VerticalAlignment.Top));
            visCollec.Add(bc = GetResizeThumb(Cursors.SizeNS, HorizontalAlignment.Center, VerticalAlignment.Bottom));
           // visCollec.Add(mov = GetMoveThumb());
        }



        protected override Size ArrangeOverride(Size finalSize)
        {
            double offset = THUMB_SIZE / 2;
            Size sz = new Size(THUMB_SIZE, THUMB_SIZE);
            tl.Arrange(new Rect(new Point(-offset, -offset), sz));
            // tl.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            tr.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width - offset, -offset), sz));
            // tr.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            bl.Arrange(new Rect(new Point(-offset, AdornedElement.RenderSize.Height - offset), sz));
            //  bl.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            br.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width - offset, AdornedElement.RenderSize.Height - offset), sz));
            // br.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            rc.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width - offset, (AdornedElement.RenderSize.Height - offset) / 2), sz));
            // rc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            lc.Arrange(new Rect(new Point(-offset, (AdornedElement.RenderSize.Height - offset) / 2), sz));
            // lc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            tc.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width / 2 - THUMB_SIZE / 2, -offset), sz));
            // tc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            bc.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width / 2 - THUMB_SIZE / 2, AdornedElement.RenderSize.Height - offset), sz));
            //bc.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
           // mov.Arrange(new Rect(new Point(AdornedElement.RenderSize.Width / 2 - THUMB_SIZE / 2, -(MOVE_OFFSET * 2)), sz));
            //  mov.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(tl_PreviewMouseLeftButtonUp);
            return finalSize;
        }

        void tl_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try { model.SaveToImageData(App.uiobj as Image, App.dt); }
            catch { }
            try { model.SaveToRectData(App.uiobj as Rectangle, App.dt); }
            catch { }

        }

        void Resize(FrameworkElement ff)
        {
            if (Double.IsNaN(ff.Width))
                ff.Width = ff.RenderSize.Width;
            if (Double.IsNaN(ff.Height))
                ff.Height = ff.RenderSize.Height;
        }

        Thumb GetMoveThumb()
        {
            var thumb = new Thumb()
            {
                Width = THUMB_SIZE,
                Height = THUMB_SIZE,
                Cursor = Cursors.SizeAll,
                Template = new ControlTemplate(typeof(Thumb))
                {
                    VisualTree = GetFactory(new SolidColorBrush(Colors.Green))
                }
            };
            //thumb.DragDelta += (s, e) =>
            //{
            //    var element = AdornedElement as FrameworkElement;
            //    if (element == null)
            //        return;

            //    Canvas.SetLeft(element, double.Parse((Canvas.GetLeft(element) + e.HorizontalChange).ToString("F0")));
            //    Canvas.SetTop(element, double.Parse((Canvas.GetTop(element) + e.VerticalChange).ToString("F0")));
            //};
            return thumb;
        }

        Thumb GetResizeThumb(Cursor cur, HorizontalAlignment hor, VerticalAlignment ver)
        {
            var thumb = new Thumb()
            {
                Background = Brushes.Red,
                Width = THUMB_SIZE,
                Height = THUMB_SIZE,
                HorizontalAlignment = hor,
                VerticalAlignment = ver,
                Cursor = null,//cur,
                Template = new ControlTemplate(typeof(Thumb))
                {
                    VisualTree = GetFactory(GetMoveEllipseBack())
                }
            };
            //thumb.DragDelta += (s, e) =>
            //{
            //    var element = AdornedElement as FrameworkElement;
            //    if (element == null)
            //        return;

            //    Resize(element);

            //    switch (thumb.VerticalAlignment)
            //    {
            //        case VerticalAlignment.Bottom:
            //            if (element.Height + e.VerticalChange > MINIMAL_SIZE)
            //            {
            //                element.Height += double.Parse(e.VerticalChange.ToString("F0"));
            //            }
            //            break;
            //        case VerticalAlignment.Top:
            //            if (element.Height - e.VerticalChange > MINIMAL_SIZE)
            //            {
            //                element.Height -= double.Parse(e.VerticalChange.ToString("F0"));
            //                Canvas.SetTop(element, double.Parse((Canvas.GetTop(element) + e.VerticalChange).ToString("F0")));
            //            }
            //            break;
            //    }
            //    switch (thumb.HorizontalAlignment)
            //    {
            //        case HorizontalAlignment.Left:
            //            if (element.Width - e.HorizontalChange > MINIMAL_SIZE)
            //            {
            //                element.Width -= double.Parse(e.HorizontalChange.ToString("F0"));
            //                Canvas.SetLeft(element, double.Parse((Canvas.GetLeft(element) + e.HorizontalChange).ToString("F0")));
            //            }
            //            break;
            //        case HorizontalAlignment.Right:
            //            if (element.Width + e.HorizontalChange > MINIMAL_SIZE)
            //            {
            //                element.Width += double.Parse(e.HorizontalChange.ToString("F0"));
            //            }
            //            break;
            //    }

            //    e.Handled = true;
            //};
            return thumb;
        }
        Brush GetMoveEllipseBack()
        {
            string lan = "M 0,5 h 10 M 5,0 v 10";
            var converter = TypeDescriptor.GetConverter(typeof(Geometry));
            var geometry = (Geometry)converter.ConvertFrom(lan);
            TileBrush bsh = new DrawingBrush(new GeometryDrawing(Brushes.Transparent, new Pen(Brushes.White, 2), geometry));
            bsh.Stretch = Stretch.Fill;
            return bsh;
        }

        FrameworkElementFactory GetFactory(Brush back)
        {
            back.Opacity = 0.6;
            var fef = new FrameworkElementFactory(typeof(Ellipse));
            fef.SetValue(Ellipse.FillProperty, back);
            fef.SetValue(Ellipse.StrokeProperty, Brushes.Gray);
            fef.SetValue(Ellipse.StrokeThicknessProperty, (double)1);
            return fef;
        }

        protected override Visual GetVisualChild(int index)
        {
            return visCollec[index];
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return visCollec.Count;
            }
        }

    }
}
